package edu.ucsd.cse135.gas.action.applicant;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import edu.ucsd.cse135.gas.logic.support.Location;
import edu.ucsd.cse135.gas.resources.Constants;

public class CountriesAction extends Action {

	public CountriesAction() {
		super();
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
	
		Location locations = new Location();
		ArrayList<Location> countries = new ArrayList<Location>();	
		countries = locations.getCountries();
		
		ArrayList<HashMap<String, String>>  countriesCol1 = new ArrayList<HashMap<String, String>>();
		ArrayList<HashMap<String, String>>  countriesCol2 = new ArrayList<HashMap<String, String>>();
		ArrayList<HashMap<String, String>>  countriesCol3 = new ArrayList<HashMap<String, String>>();
							
		/*** Prepare auxiliary iterator 'col' to get a three-column format ***/
								
		int col = countries.size()/3;
												
		/*** Iteration for column one ***/
							
		for(int i=0; i<col; i++)
		{
			HashMap<String, String> country = new HashMap<String, String>();
			
			country.put("name", countries.get(i).getName());
			country.put("id", ""+countries.get(i).getId());
			
			countriesCol1.add(country);
		}
					
		/*** Iteration for column two ***/
						
		for(int i=col; i<(col+col); i++)
		{
			HashMap<String, String> country = new HashMap<String, String>();
			
			country.put("name", countries.get(i).getName());
			country.put("id", ""+countries.get(i).getId());
			
			countriesCol2.add(country);
		}
							
									
		/*** Iteration for column three ***/
							
		for(int i=(col+col); i<countries.size(); i++)
		{
			HashMap<String, String> country = new HashMap<String, String>();
			
			country.put("name", countries.get(i).getName());
			country.put("id", ""+countries.get(i).getId());
			
			countriesCol3.add(country);
		}
		
	
		request.setAttribute("countriesCol1", countriesCol1);
		request.setAttribute("countriesCol2", countriesCol2);
		request.setAttribute("countriesCol3", countriesCol3);
		
		if(request.getRequestURL().toString().contains("name"))
		{
			return mapping.findForward(Constants.COUNTRY_OF_CITIZENSHIP);
		}
		else
		{
			return mapping.findForward(Constants.COUNTRY_OF_RESIDENCE);
		}
	}
}
